package org.bysun.ggcip.http;

import org.bysun.ggcip.Commons;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLPeerUnverifiedException;
import javax.net.ssl.SSLSession;
import javax.security.cert.X509Certificate;

/**
 * User: bysun
 * Date: 2014/6/7 0007
 * Time: 15:30
 */
public class MyX509HostnameVerifier implements HostnameVerifier {

    @Override
    public boolean verify(String s, SSLSession sslSession) {
        try {
            boolean useful = false;
            for (X509Certificate x509Certificate : sslSession.getPeerCertificateChain()) {
                String name = x509Certificate.getSubjectDN().getName().toLowerCase();
                if(name.contains(Commons.PPS.getProperty("google.cert.keyinfo","cn=google.com"))){
                    useful = true;
                }
            }
            return useful;
        } catch (SSLPeerUnverifiedException e) {
            return false;
        }
    }
}
